package lch.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import lch.constant.MessageConstant;
import lch.entity.PageResult;
import lch.entity.QueryPageBean;
import lch.entity.Result;

import lch.pojo.User;

import lch.service.CheckUserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lch
 * @date 2019-12-19 20:19
 */
@RestController
@RequestMapping("/users")
public class CheckUserController {

    @Reference//查找服务
    private CheckUserService checkUserService;

    @RequestMapping("/delete")
    public Result delete(Integer id) {

        try {
            checkUserService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "删除失败");
        }
        return new Result(true, "删除成功");
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<User> list=checkUserService.findAll();
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "查询用户失败");
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkUserService.pageQuery(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] checkroleIds) {
        try {
            checkUserService.add(user, checkroleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
        return new Result(true, "添加成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User user = checkUserService.findById(id);
            return new Result(true,"查询成功", user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @RequestMapping("/findCheckRoleIdsByCheckUserId")
    public Result findCheckRoleIdsByCheckUserId(Integer id) {
        try {
            List<Integer> list = checkUserService.findCheckRoleIdsByCheckUserId(id);
            return new Result(true, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            checkUserService.edit(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑失败");
        }
        return new Result(true, "编辑成功");
    }
}
